<template>
  <div class="company-style">
    <!-- 导航栏 start -->
    <van-nav-bar
      title="新用户注册"
      right-text="在线客服"
      left-arrow
      fixed
      placeholder
      @click-left="onBack"
      @click-right="toWXKFPerson"
    />
    <!-- 导航栏 end -->

    <!-- 内容 -->
    <div class="user">
      <van-form @submit="onSubmit" class="form">
        <van-field
          v-model="postData.UserName"
          maxlength="20"
          size="large"
          name="用户名"
          placeholder="请输入用户名，4-20位中文、字母、数字"
          :rules="[{ required: true, message: '请填写用户名' }]"
          style="font-size: 16px"
        />
        <van-field
          v-model="postData.Password"
          size="large"
          type="password"
          name="密码"
          placeholder="请输入密码"
          :rules="[{ required: true, message: '请填写密码' }]"
          style="font-size: 16px"
        />
        <van-field
          v-model="Password_check"
          size="large"
          type="password"
          name="密码"
          placeholder="请确认密码"
          :rules="[{ required: true, message: '请填写密码' }]"
          style="font-size: 16px"
        />

        <van-field
          v-model="postData.CompanyName"
          maxlength="50"
          size="large"
          name="企业名称"
          placeholder="请输入企业名称"
          :rules="[{ required: true, message: '请填写企业名称' }]"
          style="margin-top: 25px; font-size: 16px"
        />
        <van-field
          v-model="postData.LinkMan"
          maxlength="50"
          size="large"
          name="联系人"
          placeholder="请输入联系人"
          :rules="[{ required: true, message: '请填写联系人' }]"
          style="font-size: 16px"
        />
        <van-field
          v-model="postData.Tel"
          type="tel"
          maxlength="11"
          size="large"
          name="手机号"
          placeholder="请输入手机号"
          :rules="[{ required: true, message: '请填写手机号' }]"
          style="font-size: 16px"
        />
        <van-field
          v-model="postData.YZM"
          maxlength="6"
          type="number"
          center
          clearable
          placeholder="请输入验证码"
          style="font-size: 16px"
        >
          <template #button>
            <van-button
              size="small"
              type="primary"
              class="yzm"
              native-type="button"
              :disabled="enable_btn_getzym"
              :loading="loading_btn_getzym"
              @click="onGetYZM"
              >{{ btn_getyzm_title }}</van-button
            >
          </template>
        </van-field>
        <van-field name="checkbox" class="read">
          <template #input>
            <van-checkbox
              v-model="checked"
              checked-color="#07f"
              icon-size="16px"
            >
              <span>我同意</span>
              <span @click="onUserXY('xy')">《嘉兴人才网用户协议》</span>
              <span @click="onUserXY('zc')">《隐私政策》</span>
            </van-checkbox>
          </template>
        </van-field>
        <div class="btns">
          <van-button
            round
            block
            type="info"
            native-type="submit"
            :loading="loading_submit"
            :disabled="disable_submit"
            >立即注册</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import {
  NavBar,
  Form,
  Field,
  Button,
  Checkbox,
  CheckboxGroup,
  Toast,
} from "vant";
import { mixin_wx, mixin_validate } from "@/mixin/mixins";
import { CompanyGetSMSYZM, CompanyReg } from "@/util/http";
export default {
  name: "RegPage",
  components: {
    [NavBar.name]: NavBar,
    [Form.name]: Form,
    [Field.name]: Field,
    [Button.name]: Button,
    [Checkbox.name]: Checkbox,
    [CheckboxGroup.name]: CheckboxGroup,
  },
  mixins: [mixin_wx, mixin_validate],
  // 定义属性
  data() {
    return {
      Password_check: "",

      btn_getyzm_title: "获取验证码", //获取验证码按钮文字
      enable_btn_getzym: false,
      loading_btn_getzym: false,
      checked: false,
      postData: {
        UserName: "",
        Password: "",
        CompanyName: "",
        LinkMan: "",
        Tel: "",
        YZM: "",
      },
      loading_submit: false,
    };
  },
  computed: {
    disable_submit() {
      if (
        this.postData.UserName.trim().length > 0 &&
        this.postData.Password.trim().length > 0 &&
        this.postData.CompanyName.trim().length > 0 &&
        this.postData.Tel.trim().length > 0 &&
        this.postData.LinkMan.trim().length > 0 &&
        this.postData.YZM.trim().length > 0 &&
        this.Password_check.trim().length > 0 &&
        this.checked
      )
        return false;
      return true;
    },
  },
  // 方法集合
  methods: {
    onUserXY(type) {
      this.$router.push({ name: "person-login-xy", query: { type } });
    },
    //获取验证码
    onGetYZM() {
      if (this.postData.Tel.trim().length === 0) {
        Toast("请输入手机号");
        return;
      }
      if (!this.validate_phone.test(this.postData.Tel)) {
        Toast("手机号格式不正确");
        return;
      }
      this.loading_btn_getzym = true;
      CompanyGetSMSYZM(this.postData.Tel, "reg").then((res) => {
        // console.log(res);
        this.loading_btn_getzym = false;
        Toast(res.msg);
        if (res.result === 1) {
          this.getYZMSuccess();
        }
      });
    },
    getYZMSuccess() {
      let count = 60;
      let timmer = setInterval(() => {
        console.log("timmer");
        this.btn_getyzm_title = `${count}s`;
        this.enable_btn_getzym = true;
        count--;
        if (count < 0) {
          this.btn_getyzm_title = "获取验证码";
          this.enable_btn_getzym = false;
          clearInterval(timmer);
        }
      }, 1000);
    },
    onSubmit() {
      if (this.postData.Password.trim().length < 6) {
        Toast("密码至少为6位");
        return;
      }
      if (this.postData.Password != this.Password_check) {
        Toast("两次密码不一致");
        return;
      }
      if (!this.validate_phone.test(this.postData.Tel)) {
        Toast("手机号格式不正确");
        return;
      }
      this.loading_submit = true;
      CompanyReg(this.postData).then((res) => {
        // console.log(res);
        this.loading_submit = false;
        Toast(res.msg);
        if (res.result === 1) {
          this.$router.replace({
            name: "company-kf-restarted",
            params: { isReg: true, companyid: "" },
          });
        }
      });
    },
  },
};
</script>

<style lang="less">
@import "vant/lib/nav-bar/index.less";
@import "@/assets/css/company.css";
@import "@/assets/css/common.less";
</style>
